package com.yegs.hospital.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.yegs.hospital.builder.WxMaServiceBuilder;
import com.yegs.hospital.common.PageVo;
import com.yegs.hospital.dao.CaptchaDao;
import com.yegs.hospital.dao.TaskDao;
import com.yegs.hospital.dao.UserDao;
import com.yegs.hospital.service.UserService;
import com.yegs.hospital.util.ConfigConstant;
import com.yegs.hospital.vo.Captcha;
import com.yegs.hospital.vo.Task;
import com.yegs.hospital.vo.WxUser;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private WxMaServiceBuilder wxMaServiceBuilder;

    @Autowired
    private UserDao userDao;

    @Autowired
    private TaskDao taskDao;

    @Autowired
    private CaptchaDao captchaDao;

    @Override
    public WxUser login(String jsCode) throws WxErrorException {
        WxMaService wxMaService = wxMaServiceBuilder.buildWxMaService();
        WxMaJscode2SessionResult jscode2session = wxMaService.jsCode2SessionInfo(jsCode);
        WxUser wxUser = userDao.findWxUserByOpenId(jscode2session.getOpenid());
        if(wxUser == null) {
            //新增微信用户
            wxUser = new WxUser();
            wxUser.setOpenId(jscode2session.getOpenid());
            wxUser.setSessionKey(jscode2session.getSessionKey());
            wxUser.setUnionId(jscode2session.getUnionid());
        }else {
            //更新SessionKey
            wxUser.setOpenId(jscode2session.getOpenid());
            wxUser.setSessionKey(jscode2session.getSessionKey());
            wxUser.setUnionId(jscode2session.getUnionid());
        }
        return wxUser;
    }

    @Override
    public WxUser login(String username, String password) throws WxErrorException {
        return null;
    }

    @Override
    public WxUser findByOpenId(String openId) {
        return null;
    }

    @Override
    public void save(WxUser wxUser) throws Exception {
        Task task = taskDao.findTaskByStatusEqualsAndOpenIdEquals(ConfigConstant.STA_TASK_1, wxUser.getOpenId());
        if(task == null){
            task = new Task();
            if(wxUser.getCode() == null || wxUser.getCode().isEmpty()){

            }else{
                List<Captcha> all = captchaDao.findAll();
                if(all.size() > 0){
                    Captcha captcha = all.get(all.size() -1 );
                    String code = captcha.getCode();
                    if(!code.equalsIgnoreCase(wxUser.getCode())){
                        throw new Exception("验证码错误!");
                    }
                }
            }
        }
        task.setBedno(wxUser.getBedno());
        task.setOpenId(wxUser.getOpenId());
        task.setStartTime(LocalDateTime.now());
        task.setStatus(ConfigConstant.STA_TASK_1);
        task.setType(wxUser.getType());
        task.setOpenId(wxUser.getOpenId());
        task.setCreateTime(LocalDateTime.now());
        task.setDepartment(wxUser.getDepartment());
        task.setUsername(wxUser.getUsername());
        task.setIdcard(wxUser.getIdcard());
        task.setSex(wxUser.getSex());
        task.setPhone(wxUser.getPhone());
        taskDao.save(task);
        userDao.save(wxUser);
    }

    @Override
    public PageImpl<WxUser> list(PageVo<WxUser> page) {
        PageRequest request = PageRequest.of(page.getPageNumber(), page.getPageSize());
        return (PageImpl<WxUser>) userDao.findAll(request);
    }
}
